package com.ivo.controller.hr;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.common.ext.AsyncTreeNode;
import com.ivo.model.hr.Department;
import com.ivo.model.hr.Employee;
import com.ivo.service.hr.HrService;

@Controller
public class OrganizationController
{
	private HrService hrService;

	public OrganizationController() {
	}

	@Autowired
	public OrganizationController( HrService hrService ) {
		this.hrService = hrService;
	}

	@RequestMapping( "/hr/org.go" )
	public ModelAndView excute( HttpServletRequest request, HttpServletResponse response, ModelMap modelMap ) throws ServletException, IOException {

		String deptId = request.getParameter( "node" );

		JSONObject jsonObject = null;
		StringBuffer jsonString = new StringBuffer();

		Department root = hrService.getDepartment( deptId );

		List<Employee> subEmployees = hrService.getDutyEmpByDept( deptId );
		// List<Department> subDepartments = hrService.getChildDepts(deptId);
		List<Department> subDepartments = hrService.getFindChildDeptFlat( deptId );

		jsonString.append( "[" );

		Employee mgr = hrService.getEmployee( root.getHead_FK() );
		String mgr_Id = mgr.getId();

		if( mgr.getValidFlag() == 1 ) {
			AsyncTreeNode mgrNode = new AsyncTreeNode( mgr.getId() + "", mgr.getName() + " " + mgr.getName_EN(), "", "cls_manager", true );
			jsonObject = JSONObject.fromObject( mgrNode );
			jsonString.append( jsonObject.toString() );
		}
		if( subEmployees.size() > 0 ) {
			AsyncTreeNode childEmp = null;
			for( int i = 0; i < subEmployees.size(); i++ ) {
				Employee emp = subEmployees.get( i );
				String emp_Id = emp.getId();

				if( emp_Id.equals( mgr_Id ) ) continue;
				else jsonString.append( "," );

				childEmp = new AsyncTreeNode( emp_Id, emp.getName() + " " + emp.getName_EN(), "", "cls_employee", true );
				jsonObject = JSONObject.fromObject( childEmp );
				jsonString.append( jsonObject.toString() );
			}
		}
		if( subDepartments.size() > 0 ) {
			if( mgr.getValidFlag() == 1 || subEmployees.size() > 0 ) jsonString.append( "," );
			AsyncTreeNode childDept = null;
			for( int i = 0; i < subDepartments.size(); i++ ) {
				Department dept = subDepartments.get( i );

				if( i != 0 ) jsonString.append( "," );

				String strTransparentDeptInfo = "";
				if( dept.getTransparentFlag() == 1 ) {
					childDept = new AsyncTreeNode( dept.getId(), dept.getDeptName( " " ), "dept", "cls_Dept", false );
					jsonObject = JSONObject.fromObject( childDept );
				} else {
					strTransparentDeptInfo = this.getTransparentDeptInfo( dept, jsonObject );
					jsonString.append( strTransparentDeptInfo );
					jsonObject = null;
				}

				jsonString.append( ( jsonObject == null ) ? "" : jsonObject.toString() );
			}
		}
		jsonString.append( "]" );
		// System.out.println(jsonString.toString());
		return new ModelAndView( "ajax", "outString", jsonString.toString() );

	}

	public String getTransparentDeptInfo( Department dept, JSONObject jsonObject ) {
		StringBuffer deptInfo = new StringBuffer();
		AsyncTreeNode childTreeNode = null;

		List<Employee> subEmployees = hrService.getDutyEmpByDept( dept.getId() );
		Employee mgr = hrService.getEmployee( dept.getHead_FK() + "" );
		String mgr_Id = mgr.getId();

		if( dept.getCollapseFlag() == 1 ) {
			if( mgr.getValidFlag() == 1 ) {
				AsyncTreeNode mgrNode = new AsyncTreeNode( mgr.getId() + "", mgr.getName( "/ " ), "", "cls_manager", true );
				jsonObject = JSONObject.fromObject( mgrNode );
				deptInfo.append( jsonObject.toString() );
			}

			if( subEmployees.size() > 0 ) {
				AsyncTreeNode childEmp = null;
				for( int i = 0; i < subEmployees.size(); i++ ) {
					Employee emp = subEmployees.get( i );
					String emp_Id = emp.getId();

					if( emp_Id.equals( mgr_Id ) ) continue;
					else deptInfo.append( "," );

					childEmp = new AsyncTreeNode( emp_Id, emp.getName( "/ " ), "", "cls_employee", true );
					jsonObject = JSONObject.fromObject( childEmp );
					deptInfo.append( jsonObject.toString() );
				}
			}
		}
		List<Department> transparentChilds = hrService.getFindChildDeptFlat( dept.getId() );
		if( transparentChilds.size() > 0 ) {
			if( dept.getCollapseFlag() == 1 && ( mgr.getValidFlag() == 1 || subEmployees.size() > 0 ) ) deptInfo.append( "," );

			for( int i = 0; i < transparentChilds.size(); i++ ) {
				Department objDept = transparentChilds.get( i );
				childTreeNode = new AsyncTreeNode( objDept.getId(), objDept.getDeptName( "/ " ), "dept", "cls_Dept", false );
				jsonObject = JSONObject.fromObject( childTreeNode );

				if( i != 0 ) deptInfo.append( "," );
				deptInfo.append( jsonObject.toString() );
			}
		}

		// System.out.println(deptInfo.toString());
		return deptInfo.toString();
	}

}
